package com.chuji.crmcj.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.chuji.crmcj.utils.UIUtils;
import com.chuji.crmcj.utils.VolleyUtil;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 描述：
 * <p/>
 * BaseFragment 对功能菜单按钮的界面抽取实现
 *
 * @author Administrator
 */
public abstract class BaseFragment extends Fragment {
    public FrameLayout mFlLoading;
    public FrameLayout mFlLoadError;
    public View view;
    public Context mContext;
    public FrameLayout fl_loading;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        /**
         * 抽象的intview
         */
        view = initView(inflater, container, savedInstanceState);
        /**
         * 初始化数据
         */
        initData();
        /**
         * 初始化监听
         */
        initListener();
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();

    }

    public void initListener() {
    }

    /**
     * 初始化数据
     */
    public abstract void initData();

    /**
     * 初始化视图
     *
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    public abstract View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);

    /**
     * 切换fragment界面
     *
     * @param fragment MainActivity中fragment的静态常量
     */
    public void changeFragment(int fragment, Bundle args) {
//        mActivity.toFragment(fragment, args);
    }

//    /**
//     * fragment之间的切换
//     *
//     * @param fragment
//     * @param args
//     */
//    protected void changeFragment(Fragment fragment, Bundle args) {
//
//        if (args != null) {
//            fragment.setArguments(args);
//        }
//        FragmentManager fm = getActivity().getSupportFragmentManager();
//        FragmentTransaction beginTransaction = fm.beginTransaction();
//        beginTransaction.replace(R.id.fl_container, fragment);
//        beginTransaction.commit();
//    }

    /**
     * 抽取网络请求
     */
    public void volleyRequest(String url, Response.Listener<String> listener) {
//        int networkAvailable = CommonUtil.isNetworkAvailable(UIUtils.getContext());
//        if (networkAvailable == 0) {
//            //无网络
//        } else {

        StringRequest request = new StringRequest(Request.Method.GET, url, listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                UIUtils.showToastSafe("失败" + volleyError);
            }
        });
        VolleyUtil.getRequestQueue().add(request);
//        }
    }

    /**
     * post  Object 请求
     *
     * @param url
     * @param obj
     * @param listener
     */
    public void postRequest(String url, Map<String, String> obj, Response.Listener<JSONObject> listener) {
//        Map<String, String> obj = new HashMap<String, String>();
//        obj.put("SalesID", "F248611C-CB39-4806-919E-71DCD085D208");
//        int networkAvailable = CommonUtil.isNetworkAvailable(UIUtils.getContext());
//        if (networkAvailable == 0) {
//            //无网络

//        } else {
        Map<Object, Object> params = new HashMap<Object, Object>();
        params.put("ApiParamObj", obj);
        params.put("ErrCode", "0");
        params.put("Message", "POST");
        params.put("Success", "1");
        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST, url, new JSONObject(params), listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                UIUtils.showToastSafe("失败" + error.toString());
            }
        });
        VolleyUtil.getRequestQueue().add(jsonObjReq);
    }
//    }
}
